Load all required libraries.

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.3
## -- Attaching packages ---------------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.0
## v tidyr   1.1.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## Warning: package 'ggplot2' was built under R version 3.6.3
## Warning: package 'tibble' was built under R version 3.6.3
## Warning: package 'readr' was built under R version 3.6.3
## Warning: package 'dplyr' was built under R version 3.6.3
## Warning: package 'forcats' was built under R version 3.6.3
## -- Conflicts ------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## Warning: package 'plotly' was built under R version 3.6.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)
## Warning: package 'broom' was built under R version 3.6.3

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: Ignoring 1 observations
        p2
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.

Combine the two main plot pieces as a subplot

p_combined <-
    plotly::subplot(p2,p1, # plots to combine, top to bottom
      nrows = 2,
      heights = c(.6,.4),  # relative heights of the two plots
      shareX = TRUE,  # plots will share an X axis
      titleY = TRUE
    ) %>%
    # create a vertical "spike line" to compare data across 2 plots
    plotly::layout(
      xaxis = list(
        spikethickness = 1,
        spikedash = "dot",
        spikecolor = "black",
        spikemode = "across+marker",
        spikesnap = "cursor"
      ),
      yaxis = list(spikethickness = 0)
    )
## Warning: Ignoring 1 observations
p_combined

Save the plot to pull into the index

#save(p_combined, file = "./plotly_fig.rda")

Save an htmlwidget for website embedding

#htmlwidgets::saveWidget(p_combined, "plotly_fig.html")
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#build a function here to make smooth frames so we don't repeat everything in huge loops
#FOR INDIVIDUAL FIGURES ONLY
make_n1_smooth_frame <- function(df){
  smooth_n1 <- df %>% select(-c(Facility)) %>% 
  group_by(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke) %>%
  summarize(sum_copy_num_L = sum(mean_total_copies)) %>%
  ungroup() %>%
  mutate(log_sum_copies_L = log10(sum_copy_num_L)) %>%
  mutate(target = "N1")
  return(smooth_n1)
}

make_n2_smooth_frame <- function(df){
  smooth_n1 <- df %>% select(-c(Facility)) %>% 
  group_by(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke) %>%
  summarize(sum_copy_num_L = sum(mean_total_copies)) %>%
  ungroup() %>%
  mutate(log_sum_copies_L = log10(sum_copy_num_L)) %>%
  mutate(target = "N2")
  return(smooth_n1)
}

#run frames through the functions
wrfa_smooth_n1 <- make_n1_smooth_frame(wrf_a_only_n1)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
wrfb_smooth_n1 <- make_n1_smooth_frame(wrf_b_only_n1)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
wrfc_smooth_n1 <- make_n1_smooth_frame(wrf_c_only_n1)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
wrfa_smooth_n2 <- make_n2_smooth_frame(wrf_a_only_n2)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
wrfb_smooth_n2 <- make_n2_smooth_frame(wrf_b_only_n2)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
wrfc_smooth_n2 <- make_n2_smooth_frame(wrf_c_only_n2)
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
#get max date
maxdate <- max(wrfa_smooth_n1$date)
mindate <- min(wrfa_smooth_n1$date)

Build loess smoothing figures figures

#COMBINED FIGURE ONLY
#create smoothing data frames 
#n1
smooth_n1 <- only_n1 %>% select(-c(Facility)) %>% 
  group_by(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke) %>%
  summarize(sum_copy_num_L = sum(mean_total_copies)) %>%
  ungroup() %>%
  mutate(log_sum_copies_L = log10(sum_copy_num_L)) %>%
  mutate(target = "N1")
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
#n2
smooth_n2 <- only_n2 %>% select(-c(Facility)) %>% 
  group_by(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke) %>%
  summarize(sum_copy_num_L = sum(mean_total_copies)) %>%
  ungroup() %>%
  mutate(log_sum_copies_L = log10(sum_copy_num_L)) %>%
  mutate(target = "N2")
## `summarise()` regrouping output by 'date', 'cases_cum_clarke', 'new_cases_clarke', 'X7_day_ave_clarke' (override with `.groups` argument)
#**************************************COMBINED PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#n1 extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_n1 <- ggplot(smooth_n1, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n1<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 156)
## Warning: Ignoring unknown aesthetics: outfit
#n2 extract
extract_n2 <- ggplot(smooth_n2, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n2<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 156)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#n1
extract_n1
## `geom_smooth()` using formula 'y ~ x'

fit_n1
##   [1] 11.58766 11.66751 11.74673 11.82456 11.90023 11.97298 12.04206 12.10670
##   [9] 12.16756 12.22589 12.28181 12.33543 12.38687 12.43625 12.48368 12.52927
##  [17] 12.57315 12.61543 12.65623 12.69566 12.73384 12.77088 12.80515 12.83514
##  [25] 12.86119 12.88359 12.90266 12.91873 12.93210 12.94310 12.95204 12.95923
##  [33] 12.96499 12.96964 12.97349 12.97686 12.98006 12.98341 12.98723 12.99183
##  [41] 12.99753 13.00464 13.01348 13.02219 13.02888 13.03385 13.03740 13.03981
##  [49] 13.04137 13.04238 13.04313 13.04390 13.04499 13.04670 13.04931 13.05312
##  [57] 13.05841 13.06548 13.07462 13.08612 13.10028 13.11738 13.13771 13.16158
##  [65] 13.19128 13.22835 13.27189 13.32100 13.37479 13.43236 13.49283 13.55528
##  [73] 13.61884 13.68260 13.74567 13.80716 13.86616 13.92179 13.97314 14.01933
##  [81] 14.05946 14.09264 14.11796 14.13454 14.14147 14.13666 14.11973 14.09244
##  [89] 14.05651 14.01368 13.96570 13.91428 13.86118 13.80812 13.75685 13.70910
##  [97] 13.66661 13.63110 13.60433 13.57746 13.54151 13.49806 13.44864 13.39481
## [105] 13.33813 13.28013 13.22238 13.16643 13.11382 13.06612 13.02486 12.99161
## [113] 12.96791 12.95165 12.93945 12.93101 12.92605 12.92426 12.92533 12.92898
## [121] 12.93489 12.94277 12.95232 12.96324 12.97523 12.98799 13.00122 13.01462
## [129] 13.02788 13.04072 13.05283 13.06390 13.07365 13.08177 13.08980 13.09936
## [137] 13.11029 13.12239 13.13549 13.14941 13.16396 13.17898 13.19427 13.20966
## [145] 13.22496 13.24001 13.25461 13.26880 13.28277 13.29664 13.31051 13.32449
## [153] 13.33868 13.35320 13.36815 13.38363
#n2
extract_n2
## `geom_smooth()` using formula 'y ~ x'

fit_n2
##   [1] 11.37116 11.49053 11.60834 11.72376 11.83601 11.94427 12.04774 12.14563
##   [9] 12.23859 12.32794 12.41379 12.49627 12.57551 12.65164 12.72478 12.79506
##  [17] 12.86260 12.92754 12.98999 13.05009 13.10797 13.16374 13.21575 13.26246
##  [25] 13.30419 13.34126 13.37400 13.40272 13.42774 13.44939 13.46799 13.48385
##  [33] 13.49731 13.50867 13.51825 13.52639 13.53340 13.53960 13.54532 13.55087
##  [41] 13.55657 13.56274 13.56972 13.57448 13.57418 13.56936 13.56058 13.54840
##  [49] 13.53336 13.51601 13.49691 13.47661 13.45566 13.43461 13.41402 13.39443
##  [57] 13.37640 13.36047 13.34721 13.33716 13.33088 13.32891 13.33181 13.34013
##  [65] 13.35450 13.37472 13.40015 13.43012 13.46399 13.50110 13.54078 13.58240
##  [73] 13.62528 13.66878 13.71225 13.75501 13.79643 13.83583 13.87258 13.90601
##  [81] 13.93546 13.96029 13.97983 13.99343 14.00044 14.00077 13.99528 13.98471
##  [89] 13.96980 13.95132 13.92999 13.90657 13.88181 13.85644 13.83122 13.80690
##  [97] 13.78421 13.76391 13.74674 13.72962 13.70923 13.68600 13.66036 13.63272
## [105] 13.60351 13.57314 13.54204 13.51063 13.47933 13.44856 13.41875 13.39031
## [113] 13.36367 13.33780 13.31145 13.28468 13.25758 13.23021 13.20265 13.17497
## [121] 13.14725 13.11956 13.09197 13.06455 13.03738 13.01053 12.98408 12.95809
## [129] 12.93264 12.90781 12.88367 12.86028 12.83773 12.81609 12.79495 12.77390
## [137] 12.75297 12.73220 12.71164 12.69132 12.67127 12.65155 12.63218 12.61322
## [145] 12.59468 12.57663 12.55909 12.54212 12.52574 12.50992 12.49461 12.47979
## [153] 12.46542 12.45147 12.43790 12.42468
#assign fits to a vector
n1_trend <- fit_n1
n2_trend <- fit_n2

#extract y min and max for each
limits_n1 <- ggplot_build(extract_n1)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n1 <- as.data.frame(limits_n1)
n1_ymin <- limits_n1$ymin
n1_ymax <- limits_n1$ymax

limits_n2 <- ggplot_build(extract_n2)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n2 <- as.data.frame(limits_n2)
n2_ymin <- limits_n2$ymin
n2_ymax <- limits_n2$ymax

#reassign dataframes (just to be safe)
work_n1 <- smooth_n1
work_n2 <- smooth_n2

#fill in missing dates to smooth fits
work_n1 <- work_n1 %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n1 <- work_n1$date
work_n2 <- work_n2 %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n2 <- work_n2$date

#create a new smooth dataframe to layer
smooth_frame_n1 <- data.frame(date_vec_n1, n1_trend, n1_ymin, n1_ymax)
smooth_frame_n2 <- data.frame(date_vec_n2, n2_trend, n2_ymin, n2_ymax)
#make plotlys
#**************************************COMBINED PLOT**********************************************
#plot smooth frames
p3 <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_n1, y = ~n1_trend,
                    data = smooth_frame_n1,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1,
                                  '</br> Median Log Copies: ', round(n1_trend, digits = 2),
                                  '</br> Target: N1'),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
plotly::add_lines(x = ~date_vec_n2, y = ~n2_trend,
                  data = smooth_frame_n2,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2,
                                  '</br> Median Log Copies: ', round(n2_trend, digits = 2),
                                  '</br> Target: N2'),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
plotly::add_ribbons(x ~date_vec_n1, ymin = ~n1_ymin, ymax = ~n1_ymax,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n1_ymax, digits = 2),
                                  '</br> Min Log Copies: ', round(n1_ymin, digits = 2),
                                  '</br> Target: N1'),
                    name = "",
                    line = list(color = '#1B9E77')) %>%
plotly::add_ribbons(x ~date_vec_n2, ymin = ~n2_ymin, ymax = ~n2_ymax,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n2_ymax, digits = 2),
                                  '</br> Min Log Copies: ', round(n2_ymin, digits = 2),
                                  '</br> Target: N2'),
                    name = "",
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(n1_ymin), yend = ~max(n1_ymax),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(n1_ymin), yend = ~max(n1_ymax),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(n1_ymin), yend = ~max(n1_ymax),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = smooth_n1,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65)) %>%
    plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = smooth_n2,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p3

Create final trend plot by stacking with epidemic curve

smooth_extracttest <-
    plotly::subplot(p3,p1, # plots to combine, top to bottom
      nrows = 2,
      heights = c(.6,.4),  # relative heights of the two plots
      shareX = TRUE,  # plots will share an X axis
      titleY = TRUE
    ) %>%
    # create a vertical "spike line" to compare data across 2 plots
    plotly::layout(
      xaxis = list(
        spikethickness = 1,
        spikedash = "dot",
        spikecolor = "black",
        spikemode = "across+marker",
        spikesnap = "cursor"
      ),
      yaxis = list(spikethickness = 0)
    )
## Warning: Ignoring 1 observations
smooth_extracttest
#save(smooth_extracttest, file = "./smooth_extracttest.rda")

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#n1 extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_n1a <- ggplot(wrfa_smooth_n1, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n1a<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 156)
## Warning: Ignoring unknown aesthetics: outfit
#n2 extract
extract_n2a <- ggplot(wrfa_smooth_n2, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n2a<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 156)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#n1
extract_n1a
## `geom_smooth()` using formula 'y ~ x'

fit_n1a
##   [1] 11.49969 11.51581 11.53267 11.54981 11.56675 11.58303 11.59817 11.61172
##   [9] 11.62411 11.63614 11.64788 11.65939 11.67073 11.68197 11.69317 11.70440
##  [17] 11.71571 11.72718 11.73886 11.75081 11.76312 11.77582 11.78773 11.79775
##  [25] 11.80613 11.81309 11.81887 11.82371 11.82785 11.83152 11.83495 11.83839
##  [33] 11.84207 11.84622 11.85108 11.85689 11.86388 11.87230 11.88237 11.89433
##  [41] 11.90841 11.92487 11.94392 11.96664 11.99370 12.02476 12.05949 12.09753
##  [49] 12.13855 12.18220 12.22814 12.27602 12.32551 12.37625 12.42792 12.48016
##  [57] 12.53263 12.58499 12.63689 12.68800 12.73797 12.78645 12.83312 12.87761
##  [65] 12.92304 12.97229 13.02468 13.07951 13.13610 13.19377 13.25184 13.30960
##  [73] 13.36639 13.42151 13.47428 13.52401 13.57002 13.61162 13.64812 13.67885
##  [81] 13.70310 13.72021 13.72948 13.73023 13.72177 13.70182 13.66940 13.62576
##  [89] 13.57213 13.50977 13.43993 13.36385 13.28279 13.19799 13.11069 13.02215
##  [97] 12.93362 12.84633 12.76155 12.68051 12.60447 12.53467 12.47236 12.41879
## [105] 12.37521 12.34286 12.31816 12.29657 12.27793 12.26207 12.24881 12.23800
## [113] 12.22946 12.22302 12.21852 12.21579 12.21465 12.21495 12.21650 12.21915
## [121] 12.22272 12.22705 12.23197 12.23731 12.24290 12.24857 12.25415 12.26090
## [129] 12.27008 12.28152 12.29506 12.31053 12.32778 12.34662 12.36691 12.38848
## [137] 12.41115 12.43478 12.45918 12.48421 12.50969 12.53546 12.56136 12.58722
## [145] 12.61288 12.63817 12.66293 12.68717 12.71114 12.73501 12.75892 12.78306
## [153] 12.80758 12.83264 12.85842 12.88508
#n2
extract_n2a
## `geom_smooth()` using formula 'y ~ x'

fit_n2a
##   [1] 11.18228 11.29972 11.41560 11.52914 11.63951 11.74593 11.84759 11.94370
##   [9] 12.03491 12.12254 12.20671 12.28753 12.36515 12.43969 12.51126 12.58001
##  [17] 12.64605 12.70951 12.77052 12.82920 12.88569 12.94010 12.99073 13.03603
##  [25] 13.07633 13.11193 13.14319 13.17041 13.19393 13.21408 13.23118 13.24556
##  [33] 13.25754 13.26746 13.27563 13.28240 13.28807 13.29299 13.29747 13.30185
##  [41] 13.30645 13.31159 13.31762 13.32209 13.32266 13.31975 13.31377 13.30515
##  [49] 13.29432 13.28169 13.26768 13.25272 13.23723 13.22163 13.20634 13.19179
##  [57] 13.17839 13.16657 13.15674 13.14934 13.14477 13.14347 13.14586 13.15235
##  [65] 13.16347 13.17904 13.19845 13.22107 13.24630 13.27352 13.30211 13.33146
##  [73] 13.36096 13.38998 13.41792 13.44415 13.46806 13.48904 13.50647 13.51973
##  [81] 13.52821 13.53130 13.52838 13.51883 13.50204 13.47711 13.44414 13.40394
##  [89] 13.35734 13.30517 13.24825 13.18742 13.12350 13.05733 12.98972 12.92150
##  [97] 12.85351 12.78657 12.72151 12.65915 12.60033 12.54587 12.49660 12.45335
## [105] 12.41694 12.38820 12.36637 12.34978 12.33793 12.33033 12.32648 12.32587
## [113] 12.32801 12.33240 12.33853 12.34591 12.35404 12.36242 12.37055 12.37792
## [121] 12.38404 12.38842 12.39054 12.38991 12.38604 12.37841 12.36653 12.35296
## [129] 12.34039 12.32856 12.31718 12.30599 12.29471 12.28306 12.27076 12.25756
## [137] 12.24317 12.22731 12.20972 12.19011 12.16822 12.14377 12.11648 12.08609
## [145] 12.05231 12.01487 11.97350 11.92848 11.88042 11.82952 11.77599 11.72004
## [153] 11.66187 11.60170 11.53972 11.47615
#assign fits to a vector
n1_trenda <- fit_n1a
n2_trenda <- fit_n2a

#extract y min and max for each
limits_n1a <- ggplot_build(extract_n1a)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n1a <- as.data.frame(limits_n1a)
n1_ymina <- limits_n1a$ymin
n1_ymaxa <- limits_n1a$ymax

limits_n2a <- ggplot_build(extract_n2a)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n2a <- as.data.frame(limits_n2a)
n2_ymina <- limits_n2a$ymin
n2_ymaxa <- limits_n2a$ymax

#reassign dataframes (just to be safe)
work_n1a <- wrfa_smooth_n1
work_n2a<- wrfa_smooth_n1

#fill in missing dates to smooth fits
work_n1a <- work_n1a %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n1a <- work_n1a$date
work_n2a <- work_n2a %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n2a <- work_n2a$date

#create a new smooth dataframe to layer
smooth_frame_n1a <- data.frame(date_vec_n1a, n1_trenda, n1_ymina, n1_ymaxa)
smooth_frame_n2a <- data.frame(date_vec_n2a, n2_trenda, n2_ymina, n2_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_n1a, y = ~n1_trenda,
                    data = smooth_frame_n1a,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1a,
                                  '</br> Median Log Copies: ', round(n1_trenda, digits = 2),
                                  '</br> Target: N1'),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_lines(x = ~date_vec_n2a, y = ~n2_trenda,
                  data = smooth_frame_n2a,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2a,
                                  '</br> Median Log Copies: ', round(n2_trenda, digits = 2),
                                  '</br> Target: N2'),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
plotly::add_ribbons(x ~date_vec_n1a, ymin = ~n1_ymina, ymax = ~n1_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1a, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n1_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(n1_ymina, digits = 2),
                                  '</br> Target: N1'),
                    name = "",
                    line = list(color = '#1B9E77')) %>%
plotly::add_ribbons(x ~date_vec_n2a, ymin = ~n2_ymina, ymax = ~n2_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2a, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n2_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(n2_ymina, digits = 2),
                                  '</br> Target: N2'),
                    name = "",
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(n1_ymina), yend = ~max(n1_ymaxa),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(n1_ymina), yend = ~max(n1_ymaxa),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(n1_ymina), yend = ~max(n1_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(n1_ymina), yend = ~max(n1_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfa_smooth_n1,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65)) %>%
    plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfa_smooth_n2,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#n1 extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_n1b <- ggplot(wrfb_smooth_n1, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n1b<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 156)
## Warning: Ignoring unknown aesthetics: outfit
#n2 extract
extract_n2b <- ggplot(wrfb_smooth_n2, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n2b<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 156)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#n1
extract_n1b
## `geom_smooth()` using formula 'y ~ x'

fit_n1b
##   [1] 11.24142 11.29065 11.33991 11.38839 11.43530 11.47986 11.52126 11.55872
##   [9] 11.59292 11.62517 11.65558 11.68427 11.71137 11.73699 11.76125 11.78427
##  [17] 11.80617 11.82707 11.84709 11.86634 11.88496 11.90305 11.91888 11.93085
##  [25] 11.93930 11.94457 11.94700 11.94693 11.94470 11.94064 11.93510 11.92841
##  [33] 11.92092 11.91296 11.90486 11.89698 11.88964 11.88319 11.87797 11.87431
##  [41] 11.87256 11.87304 11.87611 11.87988 11.88244 11.88405 11.88499 11.88552
##  [49] 11.88594 11.88649 11.88747 11.88913 11.89176 11.89563 11.90100 11.90815
##  [57] 11.91736 11.92890 11.94303 11.96003 11.98018 12.00375 12.03100 12.06222
##  [65] 12.10128 12.15106 12.21035 12.27793 12.35257 12.43305 12.51816 12.60668
##  [73] 12.69738 12.78904 12.88045 12.97038 13.05761 13.14092 13.21910 13.29092
##  [81] 13.35515 13.41059 13.45601 13.49020 13.51192 13.51962 13.51378 13.49621
##  [89] 13.46876 13.43324 13.39148 13.34531 13.29655 13.24703 13.19858 13.15302
##  [97] 13.11218 13.07788 13.05196 13.02411 12.98414 12.93408 12.87592 12.81168
## [105] 12.74335 12.67296 12.60250 12.53400 12.46944 12.41085 12.36022 12.31957
## [113] 12.29091 12.27201 12.25891 12.25109 12.24798 12.24904 12.25371 12.26146
## [121] 12.27172 12.28395 12.29760 12.31212 12.32697 12.34158 12.35542 12.36792
## [129] 12.37855 12.38676 12.39199 12.39369 12.39131 12.38432 12.37550 12.36784
## [137] 12.36098 12.35461 12.34839 12.34199 12.33508 12.32734 12.31842 12.30801
## [145] 12.29576 12.28135 12.26445 12.24505 12.22351 12.20002 12.17478 12.14800
## [153] 12.11986 12.09057 12.06033 12.02934
#n2
extract_n2b
## `geom_smooth()` using formula 'y ~ x'

fit_n2b
##   [1] 10.90438 10.98869 11.07221 11.15420 11.23389 11.31053 11.38337 11.45166
##   [9] 11.51601 11.57765 11.63669 11.69324 11.74742 11.79933 11.84910 11.89683
##  [17] 11.94263 11.98663 12.02893 12.06965 12.10890 12.14679 12.18178 12.21246
##  [25] 12.23914 12.26213 12.28172 12.29824 12.31199 12.32327 12.33240 12.33969
##  [33] 12.34544 12.34996 12.35356 12.35654 12.35922 12.36191 12.36491 12.36853
##  [41] 12.37308 12.37886 12.38619 12.39155 12.39163 12.38705 12.37844 12.36642
##  [49] 12.35162 12.33466 12.31617 12.29676 12.27707 12.25772 12.23933 12.22253
##  [57] 12.20794 12.19619 12.18789 12.18368 12.18418 12.19001 12.20180 12.22017
##  [65] 12.24925 12.29173 12.34613 12.41095 12.48469 12.56586 12.65297 12.74452
##  [73] 12.83900 12.93494 13.03083 13.12518 13.21649 13.30326 13.38401 13.45723
##  [81] 13.52144 13.57513 13.61681 13.64499 13.65817 13.65474 13.63554 13.60275
##  [89] 13.55855 13.50513 13.44465 13.37930 13.31125 13.24269 13.17580 13.11275
##  [97] 13.05572 13.00689 12.96845 12.92921 12.87783 12.81627 12.74644 12.67028
## [105] 12.58974 12.50674 12.42322 12.34111 12.26236 12.18889 12.12264 12.06554
## [113] 12.01953 11.98103 11.94508 11.91161 11.88055 11.85183 11.82536 11.80107
## [121] 11.77889 11.75874 11.74054 11.72423 11.70971 11.69692 11.68578 11.67622
## [129] 11.66816 11.66152 11.65622 11.65221 11.64938 11.64768 11.64788 11.65069
## [137] 11.65593 11.66344 11.67305 11.68458 11.69786 11.71272 11.72899 11.74649
## [145] 11.76506 11.78452 11.80470 11.82564 11.84754 11.87052 11.89467 11.92010
## [153] 11.94691 11.97519 12.00506 12.03660
#assign fits to a vector
n1_trendb <- fit_n1b
n2_trendb <- fit_n2b

#extract y min and max for each
limits_n1b <- ggplot_build(extract_n1b)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n1b <- as.data.frame(limits_n1b)
n1_yminb <- limits_n1b$ymin
n1_ymaxb <- limits_n1b$ymax

limits_n2b <- ggplot_build(extract_n2b)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n2b <- as.data.frame(limits_n2b)
n2_yminb <- limits_n2b$ymin
n2_ymaxb <- limits_n2b$ymax

#reassign dataframes (just to be safe)
work_n1b <- wrfb_smooth_n1
work_n2b<- wrfb_smooth_n1

#fill in missing dates to smooth fits
work_n1b <- work_n1b %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n1b <- work_n1b$date
work_n2b <- work_n2b %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n2b <- work_n2b$date

#create a new smooth dataframe to layer
smooth_frame_n1b <- data.frame(date_vec_n1b, n1_trendb, n1_yminb, n1_ymaxb)
smooth_frame_n2b <- data.frame(date_vec_n2b, n2_trendb, n2_yminb, n2_ymaxb)

#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_n1b, y = ~n1_trendb,
                    data = smooth_frame_n1b,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1b,
                                  '</br> Median Log Copies: ', round(n1_trendb, digits = 2),
                                  '</br> Target: N1'),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_lines(x = ~date_vec_n2b, y = ~n2_trendb,
                  data = smooth_frame_n2b,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2b,
                                  '</br> Median Log Copies: ', round(n2_trendb, digits = 2),
                                  '</br> Target: N2'),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
plotly::add_ribbons(x ~date_vec_n1b, ymin = ~n1_yminb, ymax = ~n1_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1b, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n1_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(n1_yminb, digits = 2),
                                  '</br> Target: N1'),
                    name = "",
                    line = list(color = '#1B9E77')) %>%
plotly::add_ribbons(x ~date_vec_n2b, ymin = ~n2_yminb, ymax = ~n2_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2b, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n2_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(n2_yminb, digits = 2),
                                  '</br> Target: N2'),
                    name = "",
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(n1_yminb), yend = ~max(n1_ymaxb),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(n1_yminb), yend = ~max(n1_ymaxb),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(n1_yminb), yend = ~max(n1_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(n1_yminb), yend = ~max(n1_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfb_smooth_n1,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65)) %>%
    plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfb_smooth_n2,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** Does not work until raw data fixed #add trendlines #extract data from geom_smooth #n1 extract # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_n1c <- ggplot(wrfc_smooth_n1, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n1c<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 142)
## Warning: Ignoring unknown aesthetics: outfit
#n2 extract
extract_n2c <- ggplot(wrfc_smooth_n2, aes(x = date, y = log_sum_copies_L)) + 
  stat_smooth(aes(outfit=fit_n2c<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 142)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#n1
extract_n1c
## `geom_smooth()` using formula 'y ~ x'

fit_n1c
##   [1] 11.13969 11.20015 11.25926 11.31686 11.37279 11.42689 11.47902 11.52900
##   [9] 11.57697 11.62319 11.66770 11.71052 11.75170 11.79127 11.82926 11.86571
##  [17] 11.90066 11.93413 11.96616 11.99680 12.02606 12.05399 12.07987 12.10308
##  [25] 12.12381 12.14229 12.15870 12.17326 12.18616 12.19762 12.20782 12.21698
##  [33] 12.22530 12.23298 12.24023 12.24724 12.24687 12.23462 12.21471 12.19135
##  [41] 12.16876 12.15114 12.14272 12.14230 12.14548 12.15166 12.16022 12.17058
##  [49] 12.18213 12.19426 12.20636 12.21785 12.22811 12.23654 12.24253 12.24549
##  [57] 12.24481 12.25016 12.26805 12.29308 12.31989 12.34310 12.35732 12.35719
##  [65] 12.34563 12.32946 12.30910 12.28498 12.25751 12.22712 12.19423 12.15926
##  [73] 12.12264 12.08479 12.04613 12.00708 11.96807 11.92952 11.88884 11.84420
##  [81] 11.79710 11.74904 11.70152 11.65606 11.61416 11.56473 11.49770 11.41608
##  [89] 11.32290 11.22119 11.11398 11.00427 10.89511 10.78951 10.69049 10.60108
##  [97] 10.52431 10.46320 10.42078 10.39600 10.38392 10.38172 10.38654 10.39555
## [105] 10.40592 10.41479 10.43290 10.47083 10.52559 10.59417 10.67356 10.76075
## [113] 10.85274 10.94651 11.03908 11.12741 11.20852 11.27939 11.33702 11.37840
## [121] 11.41449 11.45583 11.49876 11.53959 11.57466 11.60030 11.62037 11.64047
## [129] 11.65946 11.67626 11.68974 11.69879 11.70230 11.70225 11.69908 11.69086
## [137] 11.67814 11.66129 11.64066 11.61662 11.58952 11.55973
#n2
extract_n2c
## `geom_smooth()` using formula 'y ~ x'

fit_n2c
##   [1] 11.64114 11.65084 11.66082 11.67086 11.68076 11.69030 11.69928 11.70749
##   [9] 11.71512 11.72254 11.72980 11.73694 11.74403 11.75111 11.75822 11.76542
##  [17] 11.77275 11.78026 11.78801 11.79605 11.80441 11.81315 11.82179 11.82988
##  [25] 11.83754 11.84489 11.85205 11.85913 11.86627 11.87357 11.88116 11.88916
##  [33] 11.89768 11.90685 11.91678 11.92759 11.93261 11.92777 11.91707 11.90453
##  [41] 11.89414 11.88991 11.89586 11.91556 11.94842 11.99224 12.04477 12.10379
##  [49] 12.16707 12.23240 12.29753 12.36025 12.41832 12.46952 12.51163 12.54241
##  [57] 12.55964 12.59153 12.65818 12.74517 12.83810 12.92255 12.98411 13.00838
##  [65] 13.00020 12.97553 12.93664 12.88578 12.82521 12.75720 12.68400 12.60788
##  [73] 12.53110 12.45591 12.38458 12.31937 12.26253 12.21634 12.16815 12.10712
##  [81] 12.03821 11.96638 11.89659 11.83379 11.78294 11.73825 11.69109 11.64213
##  [89] 11.59207 11.54161 11.49142 11.44220 11.39463 11.34941 11.30722 11.26875
##  [97] 11.23469 11.20573 11.18256 11.17586 11.19062 11.21893 11.25294 11.28476
## [105] 11.30651 11.31031 11.29755 11.27595 11.24714 11.21274 11.17439 11.13372
## [113] 11.09235 11.05191 11.01404 10.98035 10.95248 10.93207 10.92073 10.92009
## [121] 10.92385 10.92617 10.92936 10.93576 10.94768 10.96746 10.99269 11.01986
## [129] 11.04961 11.08261 11.11950 11.16094 11.20758 11.25814 11.31249 11.37193
## [137] 11.43600 11.50450 11.57720 11.65387 11.73431 11.81828
#assign fits to a vector
n1_trendc <- fit_n1c
n2_trendc <- fit_n2c

#extract y min and max for each
limits_n1c <- ggplot_build(extract_n1c)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n1c <- as.data.frame(limits_n1c)
n1_yminc <- limits_n1c$ymin
n1_ymaxc <- limits_n1c$ymax

limits_n2c <- ggplot_build(extract_n2c)$data
## `geom_smooth()` using formula 'y ~ x'
limits_n2c <- as.data.frame(limits_n2c)
n2_yminc <- limits_n2c$ymin
n2_ymaxc <- limits_n2c$ymax

#reassign dataframes (just to be safe)
work_n1c <- wrfc_smooth_n1
work_n2c <- wrfc_smooth_n1

#fill in missing dates to smooth fits
work_n1c <- work_n1c %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n1c <- work_n1c$date
work_n2c <- work_n2c %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_n2c <- work_n2c$date

#create a new smooth dataframe to layer
smooth_frame_n1c <- data.frame(date_vec_n1c, n1_trendc, n1_yminc, n1_ymaxc)
smooth_frame_n2c <- data.frame(date_vec_n2c, n2_trendc, n2_yminc, n2_ymaxc)


#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_n1c, y = ~n1_trendc,
                    data = smooth_frame_n1c,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1c,
                                  '</br> Median Log Copies: ', round(n1_trendc, digits = 2),
                                  '</br> Target: N1'),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
   layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_lines(x = ~date_vec_n2c, y = ~n2_trendc,
                  data = smooth_frame_n2c,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2c,
                                  '</br> Median Log Copies: ', round(n2_trendc, digits = 2),
                                  '</br> Target: N2'),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
plotly::add_ribbons(x ~date_vec_n1c, ymin = ~n1_yminc, ymax = ~n1_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n1c, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n1_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(n1_yminc, digits = 2),
                                  '</br> Target: N1'),
                    name = "",
                    line = list(color = '#1B9E77')) %>%
plotly::add_ribbons(x ~date_vec_n2c, ymin = ~n2_yminc, ymax = ~n2_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_n2c, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(n2_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(n2_yminc, digits = 2),
                                  '</br> Target: N2'),
                    name = "",
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(n1_yminc), yend = ~max(n1_ymaxc),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(n1_yminc), yend = ~max(n1_ymaxc),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(n1_yminc), yend = ~max(n1_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(n1_yminc), yend = ~max(n1_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfc_smooth_n1,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65)) %>%
    plotly::add_markers(x = ~date, y = ~log_sum_copies_L,
                      data = wrfc_smooth_n2,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_sum_copies_L, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(smooth_frame_n1a, file = "./plotly_objs/smooth_frame_n1a.rda")
save(smooth_frame_n2a, file = "./plotly_objs/smooth_frame_n2a.rda")
save(smooth_frame_n1b, file = "./plotly_objs/smooth_frame_n1b.rda")
save(smooth_frame_n2b, file = "./plotly_objs/smooth_frame_n2b.rda")
save(smooth_frame_n1c, file = "./plotly_objs/smooth_frame_n1c.rda")
save(smooth_frame_n2c, file = "./plotly_objs/smooth_frame_n2c.rda")
save(date_vec_n1a, file = "./plotly_objs/date_vec_n1a.rda")
save(date_vec_n2a, file = "./plotly_objs/date_vec_n2a.rda")
save(date_vec_n1b, file = "./plotly_objs/date_vec_n1b.rda")
save(date_vec_n2b, file = "./plotly_objs/date_vec_n2b.rda")
save(date_vec_n1c, file = "./plotly_objs/date_vec_n1c.rda")
save(date_vec_n2c, file = "./plotly_objs/date_vec_n2c.rda")
save(n1_ymina, file = "./plotly_objs/n1_ymina.rda")
save(n1_ymaxa, file = "./plotly_objs/n1_ymaxa.rda")
save(n2_ymina, file = "./plotly_objs/n2_ymina.rda")
save(n2_ymaxa, file = "./plotly_objs/n2_ymaxa.rda")

save(n1_yminb, file = "./plotly_objs/n1_yminb.rda")
save(n1_ymaxb, file = "./plotly_objs/n1_ymaxb.rda")
save(n2_yminb, file = "./plotly_objs/n2_yminb.rda")
save(n2_ymaxb, file = "./plotly_objs/n2_ymaxb.rda")

save(n1_yminc, file = "./plotly_objs/n1_yminc.rda")
save(n1_ymaxc, file = "./plotly_objs/n1_ymaxc.rda")
save(n2_yminc, file = "./plotly_objs/n2_yminc.rda")
save(n2_ymaxc, file = "./plotly_objs/n2_ymaxc.rda")